package com.takwolf.libgdx.core;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;

public abstract class Game implements ApplicationListener {

    private Room room;
    private Batch batch;
    private Asset asset;
    private Viewport viewport;
    private float deltaTime;
    private float stateTime;

    public void create() {
        batch = new SpriteBatch();
        asset = new Asset();
        viewport = new Viewport();
        room = getLaunchRoom();
        if (room != null) room.show();
    }

    protected abstract Room getLaunchRoom();

    public void resize(int width, int height) {
        viewport.resize(width, height);
        if (room != null) room.resize(viewport.getWidth(), viewport.getHeight());
    }

    public void render() {
        if (room != null) {
            deltaTime = Gdx.graphics.getDeltaTime();
            stateTime += deltaTime;
            room.update();
            room.draw(batch);
        }
    }

    public void pause() {
        if (room != null) room.pause();
    }

    public void resume() {
        if (room != null) room.resume();
    }

    public void dispose() {
        if (room != null) room.hide();
        asset.dispose();
        batch.dispose();
    }

    public void setRoom(Room room) {
        if (this.room != null) {
            this.room.hide();
            deltaTime = 0;
            stateTime = 0;
        }
        this.room = room;
        if (this.room != null) {
            this.room.show();
            this.room.resize(viewport.getWidth(), viewport.getHeight());
        }
    }

    public Room getRoom() {
        return room;
    }

    public Batch getBatch() {
        return batch;
    }

    public Asset getAsset() {
        return asset;
    }

    public Viewport getViewport() {
        return viewport;
    }

    public float getDeltaTime() {
        return deltaTime;
    }

    public float getStateTime() {
        return stateTime;
    }

}
